﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using FandangoAids;

public partial class Log : UserControl
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
    private const int WM_VSCROLL = 0x115;
    private const int SB_BOTTOM = 7;

    Timer logUpdateTimer = new Timer();

    public List<Log.LogOject> logObjects = new List<LogOject>();

    public class LogOject
    {
        public string Text { get; set; }
        public SerializableDictionary<string, string> Links { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }

    public Log()
    {
        InitializeComponent();

        
        logUpdateTimer_Tick(null, null);
        logUpdateTimer.Interval = 800;
        logUpdateTimer.Tick += logUpdateTimer_Tick;
        logUpdateTimer.Start();
    }

    void logUpdateTimer_Tick(object sender, EventArgs e)
    {
        if (logObjects.Count > 0)
        {
            int selectionStart = richTextBoxWithLinking.SelectionStart;
            int selectionLength = richTextBoxWithLinking.SelectionLength;

            var newlogObjects = logObjects.ToArray();
            logObjects.Clear();
            foreach (Log.LogOject logObject in newlogObjects)
            {
                if (logObject.Links != null)
                {
                    var lineparts = Regex.Split(logObject.Text, "{LINK}");

                    var links = DictionaryToArray(logObject.Links);

                    for (int i = 0; i < lineparts.Length; i++)
                    {
                        richTextBoxWithLinking.SelectionStart = richTextBoxWithLinking.Text.Length;
                        richTextBoxWithLinking.SelectedText = lineparts[i];
                        richTextBoxWithLinking.SelectionStart = richTextBoxWithLinking.Text.Length;
                        if (i < links.Length) richTextBoxWithLinking.InsertLink(links[i].Key, links[i].Value);
                    }
                }
                else
                {
                    richTextBoxWithLinking.AppendText(logObject.Text);
                }
                richTextBoxWithLinking.AppendText(Environment.NewLine);
            }

            try
            {
                richTextBoxWithLinking.SelectionStart = selectionStart;
                richTextBoxWithLinking.SelectionLength = selectionLength;
            }
            catch { }
            SendMessage(richTextBoxWithLinking.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
        }
    }

    KeyValuePair<string, string>[] DictionaryToArray(Dictionary<string, string> dict)
    {
        List<KeyValuePair<string, string>> list = new List<KeyValuePair<string,string>>();
        foreach (KeyValuePair<string, string> pair in dict) list.Add(pair);
        return list.ToArray();
    }

    private void richTextBoxWithLinking_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        Process.Start(e.LinkText.Split('#')[1]);
    }

    public void PerformLog(string text, SerializableDictionary<string, string> links = null, bool WriteEntry = false)
    {
        var logObject = new Log.LogOject { Text = DateTime.Now.ToLongTimeString() + " - " + text, Links = links};
        logObjects.Add(logObject);
        Storage.Log.Add(logObject);
        Storage.Save();
    }
}